package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api("菜品分类控制类")
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类分页查询接口
     */
    @ApiOperation("分类分页查询接口")
    @GetMapping("/page")
    public Result<PageResult> getCategoryByPage(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分类分页查询CategoryPageQueryDTO:{}",categoryPageQueryDTO);
         PageResult pr = categoryService.getCategoryByPage(categoryPageQueryDTO);
        return Result.success(pr);
    }
    /**
     * 分类新增接口
     */
    @ApiOperation("分类新增接口")
    @PostMapping
    public Result insertCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("分类新增categoryDTO:{}",categoryDTO);
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryService.insertCategory(category);
        return Result.success();
    }
    /**
     * 根据id删除分类接口
     */
    @ApiOperation("根据id删除分类接口")
    @DeleteMapping
    public Result<Category> deleteCategoryById(Long id){
        log.info("根据id删除分类id:{}",id);
        categoryService.deleteCategoryById(id);
        return Result.success();
    }
    /**
     * 修改分类接口
     */
    @ApiOperation("修改分类接口")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类接口categoryDTO:{}",categoryDTO);
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryService.updateCategory(category);
        return Result.success();
    }
    /**
     * 启用禁用分类接口
     */
    @ApiOperation("启用禁用分类接口")
    @PostMapping("/status/{status}")
    public Result updateCategoryStatus(@PathVariable Integer status,Long id){
        categoryService.updateCategoryStatus(status,id);
        return Result.success();
    }
    /**
     * 根据类型查询分类接口  用于后续开发
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> getCategoryByType(Integer type){
        List<Category> list = categoryService.getCategoryByType(type);
        return Result.success(list);
    }
}
